package com.epam.qanda.db.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.epam.qanda.db.ConnectionManager;
import com.epam.qanda.db.dao.BaseDao;
import com.epam.qanda.db.mapping.Converter;
import com.epam.qanda.db.model.Role;

public class RoleDao implements BaseDao {
	private final String ADD_ROLE = "INSERT INTO role (name) VALUES (?)";
	private final String REMOVE_ROLE = "DELETE FROM role WHERE id = ?";
	private final String GET_BY_ID = "SELECT * FROM role WHERE id = ?";
	private final String UPDATE_ROLE = "UPDATE role SET name=? WHERE id = ?";
	private final String GET_ALL_ROLES = "SELECT * FROM role";
	private static final Logger LOG = Logger.getLogger("stdout");

	@Override
	public <T> void create(T element) {
		Role temp = (Role) element;
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection.prepareStatement(ADD_ROLE)) {
			preparedStatement.setString(1, temp.getName());
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T read(Integer id) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		T user = null;
		try (PreparedStatement statement = connection.prepareStatement(GET_BY_ID)) {
			statement.setInt(1, id);
			ResultSet rs = statement.executeQuery();
			if (rs.next())
				user = (T) Converter.<Role> fromResaultSetToObject(rs, Role.class);
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return user;
	}

	@Override
	public void delete(Integer id) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection.prepareStatement(REMOVE_ROLE)) {
			preparedStatement.setInt(1, id);
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
	}

	@Override
	public <T> void update(T element) {
		Role temp = (Role) element;
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_ROLE)) {
			preparedStatement.setInt(2, temp.getId());
			preparedStatement.setString(1, temp.getName());
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
	}

	@SuppressWarnings("unchecked")
	public <T> T getAllRoles() {
		Connection connection = ConnectionManager.getInstance().getConnection();
		List<Role> roles = new ArrayList<Role>();
		try (PreparedStatement statement = connection.prepareStatement(GET_ALL_ROLES)) {
			ResultSet rs = statement.executeQuery();
			while (rs.next()) {
				roles.add(Converter.<Role> fromResaultSetToObject(rs, Role.class));
			}
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) roles;
	}

}
